package com.atguigu.yygh.hosp.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ybl
 * @version 1.9
 * @date 2021/5/17 20:36
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

  @Autowired
  private DepartmentRepository departmentRepository;

  @Override
  public Page<Department> findPageDepartment(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
    Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
//0为第一页
    Pageable pageable = PageRequest.of(page-1, limit, sort);

    Department department = new Department();
    BeanUtils.copyProperties(departmentQueryVo, department);
    department.setIsDeleted(0);

//创建匹配器，即如何使用查询条件
    ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
        .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
        .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

//创建实例
    Example<Department> example = Example.of(department, matcher);
    Page<Department> pages = departmentRepository.findAll(example, pageable);
    return pages;
  }

  // 删除科室信息
  @Override
  public void remove(String hoscode, String depcode) {
    Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    if (department != null){
      departmentRepository.deleteById(department.getId());
    }


  }

  //根据医院编号查询科室信息
  @Override
  public List<DepartmentVo> getDeptTree(String hoscode) {
    List<DepartmentVo> result = new ArrayList<>();

    // 根据hoscode 查找科室集合
    Department departmentQuery = new Department();
    departmentQuery.setHoscode(hoscode);
    // 所有科室列表
    List<Department> deptList = departmentRepository.findAll(Example.of(departmentQuery));
    // 根据bigcode进行分组
    Map<String, List<Department>> listMap = deptList.stream().collect(Collectors.groupingBy(Department::getBigcode));

    // 对科室进行封装
    for (Map.Entry<String, List<Department>> entry : listMap.entrySet()) {
      // 封装大科室
      DepartmentVo bigDept = new DepartmentVo();
      String bigCode = entry.getKey();
      List<Department> deptList1 = entry.getValue();
      String bigname = deptList1.get(0).getBigname();
      bigDept.setDepname(bigname);
      bigDept.setDepcode(bigCode);
      // 封装children
      List<DepartmentVo> children = new ArrayList<>();
      for (Department department : deptList1) {
        DepartmentVo childVo = new DepartmentVo();
        childVo.setDepcode(department.getDepcode());
        childVo.setDepname(department.getDepname());
        children.add(childVo);
      }
      bigDept.setChildren(children);
      result.add(bigDept);
    }

    return result;
  }

  // 根据hoscode 和 depcode 查询 科室名称
  @Override
  public String getDeptName(String hoscode, String depcode) {
    Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    if (department != null){
      return department.getDepname();
    }
    return null;
  }

  // // 根据hoscode ,depcode查找科室
  @Override
  public Department getDepartment(String hoscode, String depcode) {
    return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
  }

  // 上传科室信息
  @Override
  public void save(Map<String, Object> paramMap) {
    // 获取请求参数 并封装为实体类对象
    Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);
    // 从mongodb获取科室
    Department targetDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

    if (targetDepartment != null){
      department.setIsDeleted(0);
      department.setUpdateTime(new Date());
      departmentRepository.save(department);
    }else {
      department.setCreateTime(new Date());
      department.setUpdateTime(new Date());
      department.setIsDeleted(0);
      departmentRepository.save(department);
    }

  }
}
